package cn.itcast.aspect;


import cn.itcast.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
//@Aspect
@Slf4j
public class AdviceService {

    //1.编写通知方法,通过动态代理对方法增强,找到哪些方法需要增强,写切入点表达式
/*    @Before("execution(* cn.itcast.service.impl.DeptServiceImpl.deleteById(*))")
    public void before() {

        System.out.println("我是前置通知~~~");

    }
    @After("execution(* cn.itcast.service.impl.DeptServiceImpl.deleteById(*))")
    public void after() {

        System.out.println("我是后置通知~~~");

    }*/
    @Around("execution(* cn.itcast.service.impl.DeptServiceImpl.deleteById(*))")
    public Object around(ProceedingJoinPoint pjp) {
        try {
            //1.篡改参数
            Object[] args = pjp.getArgs();
            args[0] = (Integer) args[0] + 100;
            System.out.println("前置增强");
            //2.调用目标方法
            Object result = pjp.proceed(args);
            //3.篡改返回值
            Result rs = (Result) result;
            rs.setMsg(((Result) result).getMsg() + "我篡改了你的返回值哦");
            System.out.println("返回增强");
            return rs;
        } catch (Throwable e) {
            System.out.println("异常增强");
            throw new RuntimeException(e);
        } finally {
            System.out.println("后置增强");
        }
    }
}
